/**
 * 
 */
package org.istt.dao.hibernate;

import java.sql.SQLException;
import java.util.List;

import org.displaytag.properties.SortOrderEnum;
import org.hibernate.Criteria;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Projections;
import org.hibernate.criterion.Restrictions;
import org.istt.dao.ExchangeDao;
import org.istt.domain.Exchange;
import org.joda.time.DateTime;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.orm.hibernate3.HibernateCallback;
import org.springframework.stereotype.Repository;

/**
 * @author Huy
 * 
 */
@Repository("exchangeDao")
public class ExchangeDaoHibernate extends BaseDaoHibernate<Exchange, Long>
		implements ExchangeDao {
	@Autowired
	public ExchangeDaoHibernate(SessionFactory sessionFactory) {
		super(sessionFactory);
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<Exchange> getExchangeMessageByReceiver(final Long receiverId,final int firstRecordIndex,
			final int pageSize, final SortOrderEnum sortDirection, final String sortCriterion) {
		// TODO Auto-generated method stub
		return (List<Exchange>) hibernateTemplate.execute(new HibernateCallback<Object>() {

			@Override
			public List<Exchange> doInHibernate(Session session)
					throws HibernateException, SQLException {
				
				Criteria criteria = session.createCriteria(Exchange.class);
						
				criteria.createCriteria("user").add(Restrictions.idEq(receiverId));
				criteria.addOrder(Order.desc("date"));
				
				return criteria.setFirstResult(firstRecordIndex).setMaxResults(pageSize).list();
			}
		});
	}
	
	@SuppressWarnings("unchecked")
	@Override
	public List<Exchange> getExchangeMessageByFile(final String fileId, final DateTime date) {
		// TODO Auto-generated method stub
		return (List<Exchange>) hibernateTemplate.execute(new HibernateCallback<Object>() {

			@Override
			public List<Exchange> doInHibernate(Session session)
					throws HibernateException, SQLException {
				
				Criteria criteria = session.createCriteria(Exchange.class);
						
				criteria.createCriteria("file").add(Restrictions.idEq(fileId));
				criteria.add(Restrictions.le("date", date));
				criteria.addOrder(Order.asc("date"));
				
				return criteria.list();
			}
		});
	}
	
	
	@Override
	public int getNumberOfMessageByReceiver(final Long receiverId,final boolean status) {
		// TODO Auto-generated method stub
		return hibernateTemplate.execute(new HibernateCallback<Integer>() {

			@Override
			public Integer doInHibernate(Session session)
					throws HibernateException, SQLException {
				
				Criteria criteria = session.createCriteria(Exchange.class);
				
				criteria.createCriteria("user").add(Restrictions.idEq(receiverId));
				criteria.add(Restrictions.eq("status", status))
					.setProjection(Projections.rowCount());
				return ((Long)criteria.uniqueResult()).intValue();
			}
		});
	}

	@Override
	public int getRecordCountByReceiver(final Long receiverId) {
		// TODO Auto-generated method stub
		return hibernateTemplate.execute(new HibernateCallback<Integer>() {

			@Override
			public Integer doInHibernate(Session session)
					throws HibernateException, SQLException {
				
				Criteria criteria = session.createCriteria(Exchange.class);
						
				criteria.createCriteria("user")
					.add(Restrictions.idEq(receiverId))
					.setProjection(Projections.rowCount());
				return ((Long)criteria.uniqueResult()).intValue();
			}
		});
	}


}
